import React, { Component } from 'react'
import "./index.css"
export default class Item extends Component {
    // 鼠标移入移出状态
    state={mouse:false}
    // 处理鼠标移入移出
    handleMouse=(flag)=>{
        return ()=>{
            console.log(flag)
            this.setState({mouse:flag})
        }
    }
    // 处理选项框勾选，取消勾选
    handleChecked=(id)=>{
        return (event)=>{
            const {updateTodo}=this.props;
            console.log("checked--",event.target.checked)
            updateTodo(id,event.target.checked)
        }
    }
    // 处理鼠标点击删除按钮事件
    handleMouseDelBtn(id){
        const {deleteTodo}=this.props;
        if(window.confirm("确认删除吗？")){
            deleteTodo(id)
        }
    }
    render() {
        const {id,name,done}=this.props
        const {mouse}=this.state
        return (
            <li style={{backgroundColor:mouse?"#eee":"white"}} onMouseEnter={this.handleMouse(true) } onMouseLeave={this.handleMouse(false)}>
                <label>
                    <input type="checkbox" checked={done} onChange={this.handleChecked(id)}/>
                    <span>{name}</span>
                </label>
                <button className="btn btn-danger" onClick={()=>{this.handleMouseDelBtn(id)}} style={{ display: mouse?"block":"none" }}>删除</button>
            </li>
        )
    }
}
